<template>
  <section>
    <!--新增和修改界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="addForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="层级目录" prop="dirPath">
          <el-input v-model="addForm.dirPath"></el-input>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门选择">
          <el-select v-model="addForm.managerId" placeholder="请选择">
            <el-option
                v-for="item in emps"
                :key="item.id"
                :label="item.username"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="listLoading">提交</el-button>
      </div>
    </el-dialog>


    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="部门名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="seach">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表 data对应底下的data 将=后面的属性都添加在return中并给默认值  将@xxx=后面的添加在methods的方法中 -->
    <el-table :data="departments" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column type="index" >
      </el-table-column>
      <el-table-column prop="name" label="部门名称" sortable>
      </el-table-column>
      <el-table-column prop="state" label="部门状态"  :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" sortable>
      </el-table-column>
      <el-table-column prop="dirPath" label="层级目录"  sortable>
      </el-table-column>
      <el-table-column prop="manager.username" label="部门经理"  sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" layout="sizes,prev, pager, next" @current-change="handleCurrentChange"  :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

  </section>
</template>

<script>
import {batchRemoveUser} from "../../api/api";

export default {
  data() {
    return {
      /*数组  列表展示*/
      departments:[],
      /*加载动作true就是开启*/
      listLoading:false,
      sels:[],
      total: 0,
      current: 1,
      pageSize: 5,
      filters: {
        name: ''
      },
      /*模态框的默认开启状态*/
      addFormVisible: false,
      /*规则管理*/
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      /*模态框的字段的默认属性*/
      addForm: {
          name: '',
          dirPath: '',
          state: 0,
          sn: '',
          managerId:'',
      },
      title:'',
      emps: [],
    }
  },
  methods: {
    /*新增和修改的提交方法*/
    addSubmit(){
      //  $refs 校验规则
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          /*封装数据*/
          this.addForm.manager= {
            id: this.addForm.managerId
          }
          /* this.addForm获取模态框中字段的当前属性 然后在底下方法中赋值 */
          this.$http.put("/dept",this.addForm).then((res)=>{
            let {success, message} = res.data;
              if(success){
                this.listLoading = false;
                this.$message({
                  message: message,
                  type: "success"
                })
                this.addFormVisible=false;
              }else {
                this.$message({
                  message: '添加失败了哦',
                  type: "error",
                })
              }
              this.listLoading=false;
              this.loadListData();
          })
        }
      });
    },
    seach() {
      this.current = 1;
      this.loadListData();
    },
    handleAdd() {
        this.addFormVisible=true;
        this.title="新增";
      for( var i in this.addForm){
        this.addForm[i]=''
      }
      this.addForm.state=0;
      this.addFormVisible=true;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      //重新发起请求
      this.loadListData();
    },
    /*监听分页的操作*/
    handleCurrentChange(val) {
      this.current = val;
      //重新发起请求
      this.loadListData();
    },
    /*批量删除*/
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(()=>{
        this.$http.patch("/dept",ids).then((res)=>{
          let {success, message} = res.data;
          if (success) {
            /*如果成功则关闭加载*/
            this.listLoading = false;
            this.$message({
              message: message,
              type: success,
            })
          } else {
            this.$message({
              message: '恭喜你删除失败了',
              type: error,
            })
          }
          this.loadListData();
        })
      })
  },
    //加载列表的数据
    loadListData() {
      let param = {
        currentPage: this.current,
        pageSize: this.pageSize,
        keywords: this.filters.name,
      }
      //发送axios请求   响应值  ,param)
      this.$http.post("/dept", param).then((res) => {

        this.departments = res.data.data;
        this.total = res.data.total;
      })
    },
    /*勾选框*/
    selsChange: function (sels) {
      this.sels = sels;
    },
    /*性别的三目运算*/
    formatSex: function (row, column) {
      return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
    },
    /*编辑*/
    handleEdit: function (index, row) {
      //回显数据
      for( var i in this.addForm){
        //console.debug(row[i])
        this.addForm[i]=row[i];
      }
      this.addForm.managerId=row.message;
      //设置id属性
      this.addForm.id=row.id;
      //显示弹出框
      this.addFormVisible=true;

    },
    //加载下拉列表的数据
    loadEmpList() {
      //发送axios请求   响应值  ,param)
      this.$http.get("/emp").then((res) => {
        this.emps = res.data;
      })
    },
    /*删除  index选中行的数据,row当前的数据  */
    /* $confirm 确认框  type: 'warning'确认框的颜色  */
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
        /* .then((回调函数)=>{}) 用户点击时会发生的事    */
      }).then(() => {
        /*发起之前加载动画*/
        this.listLoading = true;
        /* 向后台发起请求   请求删除 url是/dept/+id  加上需要删除的id 用 res.id获取 */
        this.$http.delete("/dept/" + row.id).then((res) => {
          /*解构表达式*/
          let {success, message} = res.data;
          if (success) {
            /*如果成功则关闭加载*/
            this.listLoading = false;
            this.$message({
              message: message,
              type: success,
            })
          } else {
            this.$message({
              message: '恭喜你删除失败了',
              type: error,
            })
          }
          //重新加载列表
          this.loadListData();
        })
      })
    }
  },
  mounted() {
    this.loadListData();
    this.loadEmpList();
  }
}


</script>